<template>
  <div class="manager-header">
    <div class="manager-header-left">
      <img src="../../assets/imgs/logo.png" alt="Logo" class="logo">
      <div class="title">植物栽培学习管理平台</div>
    </div>
    <div class="manager-header-center">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/manager/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>{{ router.currentRoute.value.meta.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="manager-header-right">
      <el-dropdown style="cursor: pointer" v-if="data.user.id">
        <div class="user-info">
          <img :src="data.user.avatar" alt="Avatar" class="avatar">
          <span class="username">{{ data.user.name }}</span>
          <el-icon color="#fff"><arrow-down /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="router.push('/manager/person')">个人资料</el-dropdown-item>
            <el-dropdown-item @click="router.push('/manager/password')">修改密码</el-dropdown-item>
            <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <el-button v-else type="text" @click="router.push('/login')" class="login-btn">登录</el-button>
    </div>
  </div>
</template>

<script setup>
import { reactive, computed } from "vue";
import router from "@/router/index.js";
import { ElMessage } from "element-plus";
import { ArrowDown } from "@element-plus/icons-vue";
import request from "@/utils/request.js";

const data = reactive({
  user: JSON.parse(localStorage.getItem('user') || '{}')
});

const currentRouteMetaName = computed(() => router.currentRoute.value.meta.name);

const logout = () => {
  request.post('/logOut').then(res => {
    localStorage.removeItem('user');
    localStorage.removeItem('token');
    router.push('/login');
    if (res.code === '200') {
      ElMessage.success('退出成功');
    } else {
      ElMessage.error(res.msg);
    }
  });
};

const updateUser = () => {
  data.user = JSON.parse(localStorage.getItem('user') || '{}');
};

if (!data.user.id) {
  router.push('/login');
  ElMessage.error('请登录！');
}
</script>

<style scoped>
.manager-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  background-color: #409EFF;
  color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.manager-header-left {
  display: flex;
  align-items: center;
}

.logo {
  width: 40px;
  height: 40px;
  margin-right: 10px;
  border-radius: 50%;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.manager-header-center {
  flex-grow: 1;
  margin-left: 20px;
}

.el-breadcrumb {
  font-size: 16px;
}

.manager-header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
}

.username {
  margin-right: 5px;
  color: white;
}

.login-btn {
  color: white;
  font-size: 16px;
}
</style>



